/**
 * Copyright ® 2006 Shenzhen Cogent Co.Ltd.
 * All right reserved.
 */
package com.cogent.common.util;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

/**
 * 
 * Date: 2006-3-17
 * Author: KKK
 */
public class RequestUtil {
	
	/**
	 * 获取地址栏服务器的地址（主机＋端口号＋虚拟目录）.
	 * @param request
	 * @return String
	 */
	public static String getURL(HttpServletRequest request) {
		String server = "http://" + request.getServerName();
		int port = request.getServerPort();
		if (port != 80) {
			server = server + ":" + port;
		}
		server += request.getContextPath() + "/";
		return server;
	}	
	
	/**
	 * 获取地址栏的地址.
	 * @param request
	 * @return
	 */
	public static String getRequestURL(HttpServletRequest request) {
		StringBuffer sb = request.getRequestURL();
        if (request.getQueryString() != null)
        	sb.append("?").append(request.getQueryString()); 
        return sb.toString();
	}
	
	public static String getReferer(HttpServletRequest request) {
		return request.getHeader("referer");
	}
	
	/**
	 * get the path what the user see
	 * @param request
	 * @param flag concat querystring or not.default true;
	 * @return
	 */
	public static String getPath(HttpServletRequest request, boolean flag) {
		String path = (String) request.getAttribute("path");
		if (flag && request.getQueryString() != null)
			path += "?" + request.getQueryString();
		return path;
	}
	
	public static String getPath(HttpServletRequest request) {
		return getPath(request, true);
	}
		
	/**
	 * 将指定消息置于session属性中，便于使用RequestUtil.outMsg(request)反馈信息.
	 * msg为config.xml文件的节点.
	 * @param request 请求.
	 * @param msg	需要显示的信息.
	 * @author MaHB
	 * @since 2006.04.14
	 */
	public static void setMsg(HttpServletRequest request,String msg){
		msg = Globals.getPropertyNotNull(msg);
		setMessage(request, msg);
	}
	
	/**
	 * 将指定消息置于session属性中，便于使用RequestUtil.outMsg(request)反馈信息.
	 * msg为显示的内容.
	 * @param request
	 * @param msg
	 */
	public static void setMessage(HttpServletRequest request,String msg) {
		request.getSession().setAttribute("msg",msg);
	}	
	
	/**
	 * 获取指定传入参数并转为int
	 * @param request
	 * @param parameter 参数名.
	 * @return int
	 */
    public static int getInt(HttpServletRequest request, String parameter) {
        int result = 0;
        String str = request.getParameter(parameter);
        try {
            result = Integer.parseInt(str);
        } catch (Exception e) {
            //do nothing, just ignore this
        }
        return result;
        
    }
    
    /**
     * 获取指定传入参数并转为int
     * @param request
     * @param parameter 参数名.
     * @return Integer
     */
    public static Integer getInteger(HttpServletRequest request, String parameter) {
        int result = 0;
        String str = request.getParameter(parameter);
        try {
            result = Integer.parseInt(str);
        } catch (Exception e) {
            //do nothing, just ignore this
        }
        return new Integer(result);        
    }
    
    public static String getAttributeAsString(HttpServletRequest request, String key) {
    	String ret = "";
    	Object obj = request.getAttribute(key);
    	if(obj != null)
    		ret = obj.toString();
    	
    	return ret;
    }
    
    public static Map getAttributeAsMap(HttpServletRequest request, String key) {
    	Map ret = new HashMap();
    	Object obj = request.getAttribute(key);
    	if(obj != null)
    		ret = (Map)obj;
    	
    	return ret;
    }
    
    public static int getAttributeAsInt(HttpServletRequest request, String key) {
    	int ret = 0;
    	Object obj = request.getAttribute(key);
    	if(obj != null)
    		ret = Integer.parseInt(obj.toString());
    	
    	return ret;
    }
}
